package abg.core.common.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class LocalDateKit {
    public static final DateTimeFormatter yMd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter Hms = DateTimeFormatter.ofPattern("HH:mm:ss");
    public static final DateTimeFormatter yMdHms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDateTime now() {
        return LocalDateTime.now();
    }

    public static String nowStr() {
        return nowStr(yMdHms);
    }

    public static String nowStr(DateTimeFormatter formatStr) {
        LocalDateTime dateTime = LocalDateTime.now();
        return dateTime.format(formatStr);
    }


    public static String dateToStr(LocalDateTime date) {
        return dateToStr(date, yMdHms);
    }

    public static String dateToStr(LocalDateTime date, DateTimeFormatter formatStr) {
        return formatStr.format(date);
    }

    public static LocalDateTime strToDate(String dateStr) {
        return strToDate(dateStr, yMdHms);
    }

    public static LocalDateTime strToDate(String dateStr, DateTimeFormatter format) {
        return LocalDateTime.parse(dateStr, format);
    }

    //毫秒（含正负号）
    public static long subMilli(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        // 计算间隔的毫秒数
        return ChronoUnit.MILLIS.between(dateTimeSmall, dateTimeBig);
    }

    //秒（含正负号）
    public static long subSecond(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return ChronoUnit.SECONDS.between(dateTimeSmall, dateTimeBig);
    }

    //分钟（含正负号）
    public static long subMinute(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return ChronoUnit.MINUTES.between(dateTimeSmall, dateTimeBig);
    }

    //小时（含正负号）
    public static long subHour(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return ChronoUnit.HOURS.between(dateTimeSmall, dateTimeBig);
    }

    //月（含正负号）
    public static long subMonth(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return ChronoUnit.MONTHS.between(dateTimeSmall, dateTimeBig);
    }

    //年（含正负号）
    public static long subYear(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return ChronoUnit.YEARS.between(dateTimeSmall, dateTimeBig);
    }

    public static long subMilliAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subMilli(dateTimeSmall, dateTimeBig));
    }

    public static long subSecondAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subSecond(dateTimeSmall, dateTimeBig));
    }

    public static long subMinuteAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subMinute(dateTimeSmall, dateTimeBig));
    }

    public static long subHourAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subHour(dateTimeSmall, dateTimeBig));
    }

    public static long subMonthAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subMonth(dateTimeSmall, dateTimeBig));
    }

    public static long subYearsAbs(LocalDateTime dateTimeBig, LocalDateTime dateTimeSmall) {
        return Math.abs(subYear(dateTimeSmall, dateTimeBig));
    }

//    public static void main(String[] args) {
//    }
}
